﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Homework_6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Stack<char> vsChar = new Stack<char>();
        private Stack<double> vsDouble = new Stack<double>();
        private string words = "";
        //栈函数
        private void coStack()
        {
            string tmpnum = "";
            foreach (char c in words)
            {
                double result = 0;
                switch (c)
                {
                    case '+':
                        vsDouble.Push(double.Parse(tmpnum));
                        tmpnum = "";
                        if (vsChar.Count!=0)
                        {
                            coRes(vsChar.Peek());
                        }
                        vsChar.Push('+');
                        break;
                    case '-':
                        vsDouble.Push(double.Parse(tmpnum));
                        tmpnum = "";
                        if (vsChar.Count != 0)
                        {
                            coRes(vsChar.Peek());
                        }
                        vsChar.Push('-');
                        break;
                    case '*':
                        vsDouble.Push(double.Parse(tmpnum));
                        tmpnum = "";
                        if (vsChar.Count != 0)
                        {
                            coRes(vsChar.Peek());
                        }
                        vsChar.Push('*');
                        break;
                    case '/':
                        vsDouble.Push(double.Parse(tmpnum));
                        tmpnum = "";
                        if (vsChar.Count != 0)
                        {
                            coRes(vsChar.Peek());
                        }
                        vsChar.Push('/');
                        break;
                    case '=':
                        vsDouble.Push(double.Parse(tmpnum));
                        tmpnum = "";
                        if (vsChar.Count != 0)
                        {
                            coRes(vsChar.Peek());
                        }
                        while (vsChar.Count!=0)
                        {
                            switch (vsChar.Pop())
                            {
                                case '+':
                                    double tmp2 = vsDouble.Pop();
                                    double tmp1 = vsDouble.Pop();
                                    vsDouble.Push(tmp1 + tmp2);
                                    break;
                                case '-':
                                    double tmp22 = vsDouble.Pop();
                                    double tmp11 = vsDouble.Pop();
                                    vsDouble.Push(tmp11 - tmp22);
                                    break;
                                default:
                                    break;
                            }
                        }
                        result = vsDouble.Pop();
                        words += result;
                        button1.Text = words;
                        break;
                    default:tmpnum += c;
                        break;

                }
            }
        }

        //符号位入栈后操作函数
        private void coRes(char c)
        {
            switch (c)
            {
                case '*':
                    double tmp2 = vsDouble.Pop();
                    double tmp1 = vsDouble.Pop();
                    vsDouble.Push(tmp2 * tmp1);
                    vsChar.Pop();
                    break;
                case '/':
                    double tmp22 = vsDouble.Pop();
                    double tmp11 = vsDouble.Pop();
                    vsDouble.Push(tmp11 / tmp22);
                    vsChar.Pop();
                    break;
                default:
                    break;
            }
        }

        private void storeWord(char c)
        {
            words += c;
            button1.Text = words;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            storeWord('1');
        }

        private void button3_Click(object sender, EventArgs e)
        {
            storeWord('2');
        }

        private void button4_Click(object sender, EventArgs e)
        {
            storeWord('3');
        }

        private void button5_Click(object sender, EventArgs e)
        {
            storeWord('4');
        }

        private void button6_Click(object sender, EventArgs e)
        {
            storeWord('5');
        }

        private void button7_Click(object sender, EventArgs e)
        {
            storeWord('6');
        }

        private void button8_Click(object sender, EventArgs e)
        {
            storeWord('7');
        }

        private void button9_Click(object sender, EventArgs e)
        {
            storeWord('8');
        }

        private void button10_Click(object sender, EventArgs e)
        {
            storeWord('9');
        }

        private void button12_Click(object sender, EventArgs e)
        {
            storeWord('0');
        }

        private void button14_Click(object sender, EventArgs e)
        {
            storeWord('+');
        }

        private void button15_Click(object sender, EventArgs e)
        {
            storeWord('-');
        }

        private void button16_Click(object sender, EventArgs e)
        {
            storeWord('*');
        }

        private void button17_Click(object sender, EventArgs e)
        {
            storeWord('/');
        }

        private void button11_Click(object sender, EventArgs e)
        {
            words = "";
            button1.Text = words;
        }

        private void button13_Click(object sender, EventArgs e)
        {
            storeWord('=');
            coStack();
        }
    }
}
